package com.wonderli.wondertweet;

import java.net.URI;

import org.apache.http.cookie.Cookie;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Prefs extends Activity{
    public final String TAG = getClass().getName();
    public OAuthService service;
    public Token requestToken;
    public String authUrl;
    public Uri uri;
    public Verifier verifier;
    public Token accessToken;
    public OAuthRequest request;
    public Response response;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
//        final Button loginBtn = (Button) findViewById(R.id.twitter_login_btn);
//	      final EditText twitterName = (EditText) findViewById(R.id.twitter_account_name);
//        final EditText twitterPwd = (EditText) findViewById(R.id.twitter_account_pwd);
        final Button oauthBtn = (Button) findViewById(R.id.twitter_oauth_btn);
        final EditText pin = (EditText) findViewById(R.id.twitter_oauth_pin);
        final Button pinGoBtn = (Button) findViewById(R.id.pin_go);
        oauthBtn.setOnClickListener(new View.OnClickListener() {						
			public void onClick(View v) {
				service = new ServiceBuilder().provider(TwitterApi.class).apiKey(Constants.CONSUMER_KEY).apiSecret(Constants.CONSUMER_SECRET).build();
		        requestToken = service.getRequestToken();
		        authUrl = service.getAuthorizationUrl(requestToken);
		        //System.out.println(authUrl);
		        //Toast.makeText(OAuth.this, authUrl, 10000).show();
		        uri = Uri.parse(authUrl);
		        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		        startActivity(intent);
			}
		});
        pinGoBtn.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {				
				verifier = new Verifier(pin.getText().toString());
		        accessToken = service.getAccessToken(requestToken, verifier);
		        request = new OAuthRequest(Verb.GET, "http://api.twitter.com/1/account/verify_credentials.xml");
		        service.signRequest(accessToken, request);
		        
		        //response = request.send();
		        //System.out.println(response.getBody());
		        SharedPreferences settings = getSharedPreferences(Constants.SETTINGS,0);
		        SharedPreferences.Editor editor = settings.edit();
		        String accesstoken = accessToken.getToken();		        
		        String secrettoken = accessToken.getSecret();
		        editor.putString("accesstoken", accesstoken);
		        editor.putString("secrettoken", secrettoken);		
		        editor.commit();
		        System.out.println("looks like commited");
		        Intent intent = new Intent();
		        Bundle bundle = new Bundle();
		        bundle.putString("accesstoken", accesstoken);
		        bundle.putString("secrettoken", secrettoken);
		        intent.setClass(Prefs.this, UpdateStatus.class);
		        intent.putExtras(bundle);
		        setResult(Constants.RESULT_CODE, intent);
		        startActivityForResult(intent, Constants.REQUEST_CODE);
			}
		});
    }


}

